package com.apex.restful.service.impl;
import com.apex.restful.entity.VBsdtDb;
import com.apex.restful.entity.VBsdtYb;
import com.apex.restful.service.WorkflowRestfulService;
import com.apex.security.api.entity.TUser;
import com.apex.security.api.entity.VBsdtDbSummary;
import com.apex.util.ResponseMessage;
import com.edu.orm.datasource.DBUtil;
import com.edu.orm.entity.Pager;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.jsoniter.output.JsonStream;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WorkflowRestfulServiceImpl implements WorkflowRestfulService {

    @Autowired
    private DBUtil dbUtil;

    @Override
    public String getAllDB(String userID,String code) {
        Map<String,Object> map= ResponseMessage.getMap();
        StringBuffer sql=new StringBuffer("select * from v_bsdt_db t where 1 =1 ");
        List<Object> parame=new ArrayList<>();
        Pager pager=null;
        Any any=null;
        if(StringUtils.isNotBlank(code)){
            any= JsonIterator.deserialize(code);
            userID=any.get("userID").toString();
            String pg=any.get("pager").toString();
            if(StringUtils.isNotBlank(pg)){
                pager=any.get("pager").as(Pager.class);
            }
        }
        if(StringUtils.isNotBlank(userID)){
            List<TUser>lists=  dbUtil.getAllBeanList("select t.userid from tuser t where t.userid = ? or t.logincode= ? ",TUser.class,userID,userID);
            if(null!=lists&&lists.size()>0){
                TUser user=lists.get(0);
                userID=user.getUserid();
            }
            sql.append(" and t.userid = ? ");
            parame.add(userID);
            if(null==pager){
                pager=new Pager();
                pager.setCurrent(1);
                pager.setRows(8);
            }
            if(null!=any){
                String title=any.get("title").toString();
                if(StringUtils.isNotBlank(title)&&!"null".equals(title)){
                    sql.append(" and t.title like ? ");
                    parame.add("%"+title+"%");
                }
            }
            int count=dbUtil.count("select count(*) from ("+sql.toString()+")",parame.toArray());
            pager.setTotalPages(count);
            List<VBsdtDb> list=dbUtil.getAllPagerBeanList(pager,sql.toString(),VBsdtDb.class,parame.toArray());
            map=ResponseMessage.getTrueMap();
            map.put("dataList",list);
            map.put("pager",pager);
        }
        return JsonStream.serialize(map);
    }

    @Override
    public String getAllYB(String userID,String code) {
        Map<String,Object> map= ResponseMessage.getMap();
        StringBuffer sql=new StringBuffer("select t.id,t.url,t.title,t.time,t.name,t.userid,t.state,t.yhsf,t.sqr,t.sqrgh from V_BSDT_YB t where 1 = 1");
        List<Object> parame=new ArrayList<>();
        Pager pager=null;
        Any any=null;
        if(StringUtils.isNotBlank(code)){
            any= JsonIterator.deserialize(code);
            userID=any.get("userID").toString();
            String pg=any.get("pager").toString();
            if(StringUtils.isNotBlank(pg)){
                pager=any.get("pager").as(Pager.class);
            }
        }
        if(StringUtils.isNotBlank(userID)) {
            sql.append(" and t.userid = ? ");
            List<TUser> lists=  dbUtil.getAllBeanList("select t.userid from tuser t where t.userid = ? or t.logincode= ? ",TUser.class,userID,userID);
            if(null!=lists&&lists.size()>0){
                TUser user=lists.get(0);
                userID=user.getUserid();
            }
            parame.add(userID);
            if (null == pager) {
                pager = new Pager();
                pager.setCurrent(1);
                pager.setRows(8);
            }
            if (null != any) {
                String title = any.get("title").toString();
                if (StringUtils.isNotBlank(title)&&!"null".equals(title)) {
                    sql.append(" and t.title like ? ");
                    parame.add("%" + title + "%");
                }
            }
            sql.append(" group by t.id,t.url,t.title,t.time,t.name,t.userid,t.state,t.yhsf,t.sqr,t.sqrgh having count(*)>=1");
            int count=dbUtil.count("select count(*) from ("+sql.toString()+")",parame.toArray());
            pager.setTotalPages(count);
            List<VBsdtYb> list=dbUtil.getAllPagerBeanList(pager,sql.toString(),VBsdtYb.class,parame.toArray());
            map=ResponseMessage.getTrueMap();
            map.put("dataList",list);
            map.put("pager",pager);
        }
        return JsonStream.serialize(map);
    }

    @Override
    public String getUserSummary(String userID) {
        Map<String, Object> map = ResponseMessage.getMap();
        if(StringUtils.isNotBlank(userID)){
            String sql_sq="SELECT url,title,TIME from v_bsdt_yb where sqrgh= ? GROUP BY url,title,TIME HAVING count(*)>0";
            int sqsl=dbUtil.count("select count(*) from ("+sql_sq+")",userID);
            String sql_yb="SELECT url,title,TIME from v_bsdt_yb where sqrgh= ? and state='已完成' GROUP BY url,title,TIME HAVING count(*)>0";
            int ybl=dbUtil.count("select count(*) from ("+sql_yb+")",userID);
            String sql_wb="SELECT url,title,TIME from v_bsdt_yb where sqrgh= ? and state='处理中' GROUP BY url,title,TIME HAVING count(*)>0";
            int wb=dbUtil.count("select count(*) from ("+sql_wb+")",userID);
            String sql="select count(*) from v_bsdt_db t where t.userid = ? ";
            int dbsl=dbUtil.count(sql,userID);
            VBsdtDbSummary summary=new VBsdtDbSummary();
            map=ResponseMessage.getTrueMap();
            summary.setSqsl(sqsl);
            summary.setYbl(ybl);
            summary.setWwjsl(wb);
            summary.setDbsl(dbsl);
            map.put("data",summary);
        }
        return JsonStream.serialize(map);
    }


}
